import operator

def getcomb(num):
    data = [i for i in range(1, 14) for _ in range(4)]
    # 随机抽取num个数据生成N组数据
    N = 1000000  # 可以根据需要调整N的值
    random_combinations = [sorted(random.sample(data, num)) for _ in range(N)]
    # 使用set进行去重
    unique_combinations = list(set(tuple(comb) for comb in random_combinations))
    # 对结果进行排序
    sorted_combinations = sorted(unique_combinations)
    return  sorted_combinations


def judgePoint24(nums):
    if not nums:
        return 0, []
    results = set()  # Use a set to avoid duplicates
    dfs([(num, str(num)) for num in nums], results)
    return len(results), results

def dfs(nums, results):
    if len(nums) == 1:
        if abs(nums[0][0] - 24) < 1e-6:
            results.add(nums[0][1])  # Add the expression to the set
        return

    for i in range(len(nums)):
        for j in range(i+1, len(nums)):  # Only combine each pair of numbers once
            newNums = [nums[k] for k in range(len(nums)) if k != i and k != j]
            for op, symbol in zip((operator.add, operator.sub, operator.mul, operator.truediv), '+-*/'):
                if op is operator.truediv and abs(nums[j][0]) < 1e-6:  # Avoid division by zero
                    continue
                newNum = op(nums[i][0], nums[j][0])
                newExpression = f"({nums[i][1]} {symbol} {nums[j][1]})"
                newNums.append((newNum, newExpression))
                dfs(newNums, results)
                newNums.pop()
                if op is operator.add or op is operator.mul:  # For addition and multiplication, the order does not matter
                    newNum = op(nums[j][0], nums[i][0])
                    newExpression = f"({nums[j][1]} {symbol} {nums[i][1]})"
                    newNums.append((newNum, newExpression))
                    dfs(newNums, results)
                    newNums.pop()

#建议使用双字典
def get24dian_comb(num, limit):
    sorted_combinations = getcomb(num)
    comb_dic = {}
    for comb in sorted_combinations:
        count, results = judgePoint24(comb)
        if count == 0:
            continue
        elif count < limit:
            if count < 20:
                print("困难", count)
            elif count < 60:
                print("中等", count)
            else:
                print("简单", count)
    return  comb_dic   
              

#nums = [1, 2, 3, 4]
#count, results = judgePoint24(nums)

#get24dian_comb(4, 200)
